<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 2016/11/8
 * Time: 下午10:20
 */
class TaskService extends Service {

    public static function indexHeader($params){
        $header = array(
            'RecDate'           => array('text' => '日期'),
            'SID'               => array('text' => '服'),
            'TaskType'          => array('text' => '任务类型'),
            'TaskID'            => array('text' => '任务ID'),
            'TaskName'          => array('text' => '任务名称'),
            'GetTaskNumber'     => array('text' => '接受任务数', 'rule' => 'numeric'), //接受任务人数
        	'LastGetTaskNumber' => array('text' => '上个任务完成数', 'rule' => 'numeric'),
        	'AcceptRate'        => array('text' => '任务接受率', 'rule' => 'numeric'),
            'CompTaskNumber'    => array('text' => '完成任务数', 'rule' => 'numeric'), //完成任务人数
            'NumberRate'        => array('text' => '任务完成率', 'rule' => 'percent'),
            'GetTaskCnt'        => array('text' => '接受任务人次', 'rule' => 'numeric'),
            'CompTaskCnt'       => array('text' => '完成任务人次', 'rule' => 'numeric'),
        );
        if($params['server'] == 'ALL'){
        	unset($header['SID']);
        }

        return $header;
    }

    public static function indexData($params){
        $data = ModTask::indexData($params);

        if($params['time_start'] == $params['time_end']){
            $date = $params['time_start'];
        }else{
            $date = date('Y.m.d', strtotime($params['time_start'])).'~'.date('Y.m.d', strtotime($params['time_end']));
        }

        foreach($data as &$v){
            $v['RecDate']           = $date;
            $v['SID']               = $params['server'];

            $v['CompTaskNumber']    = empty($v['CompTaskNumber']) ? 0 : $v['CompTaskNumber'];
            
            $v['AcceptRate']        = number_format($v['GetTaskNumber'] / $v['LastGetTaskNumber'] * 100, 2).'%';
            
            $v['NumberRate']        = number_format($v['CompTaskNumber'] / $v['GetTaskNumber'] * 100, 2).'%';
        }
        unset($v);

        return $data;
    }
    
    public static function realIndexData($params){
    	$recDate = time() - time()%300;
    	$recDate = $recDate - 20*60;
    	//echo $recDate;
    	//$recDate = '1541823300';
    	$params['recDate'] = $recDate;
    	//echo $params['recDate'];
    	
    	$recDate = $params['recDate'];
    	
    	$data = ModTask::realIndexData($params);
    	
    	$lastData = array();
    	foreach ($data as $k=>$v){
    		$lastData[$v['TaskID']] = $v['CompTaskNumber'];
    	}
    	
//     	echo '<pre/>';
//     	var_dump($lastData);exit;
    	
    	
    	$date = date('Y.m.d H:i', $recDate);
    	//echo $date;
    
    	foreach($data as &$v){
    		$LastGetTaskNumber = is_null($lastData[$v['LastID']]) ? 0 : $lastData[$v['LastID']];
    		$v['LastGetTaskNumber'] = $LastGetTaskNumber;
    		
    		$v['RecDate']           = $date;
    		$v['SID']               = $params['server'];
    		
    		$v['GetTaskNumber'] = is_null($v['GetTaskNumber'])? 0 : $v['GetTaskNumber'];
    		$v['GetTaskCnt'] = is_null($v['GetTaskCnt'])? 0 : $v['GetTaskCnt'];
    		$v['CompTaskCnt'] = is_null($v['CompTaskCnt'])? 0 : $v['CompTaskCnt'];
    
    		$v['CompTaskNumber']    = empty($v['CompTaskNumber']) ? 0 : $v['CompTaskNumber'];
    		
    		$v['AcceptRate'] = $v['LastGetTaskNumber'] == 0? '0.00%' : number_format($v['GetTaskNumber'] / $v['LastGetTaskNumber'] * 100, 2).'%';
    		$v['NumberRate'] = $v['GetTaskNumber'] == 0? '0.00%' : number_format($v['CompTaskNumber'] / $v['GetTaskNumber'] * 100, 2).'%';
    
    		//$v['AcceptRate']        = number_format($v['GetTaskNumber'] / $v['LastGetTaskNumber'] * 100, 2).'%';
    		//$v['NumberRate']        = number_format($v['CompTaskNumber'] / $v['GetTaskNumber'] * 100, 2).'%';
    	}
    	unset($v);
    
    	return $data;
    }

    public static function dictTaskType(){
        return array(
            '总览' => array('' => 'ALL'),
            '明细' => PubDict::dict('roma', 'tasktype'),
        );
    }

}